package teamproject.graphEditor.graphPainters;

import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import teamproject.graphEditor.Controller;
import teamproject.graphEditor.Node;
import teamproject.graphEditor.PDEEdge;
import teamproject.graphEditor.SortCollectionsToArray;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;

public class CascadeNetworkPainter implements GraphPainter {

	private Controller control;
	private int dimension;

	public CascadeNetworkPainter(Controller control, int dimension) {
		this.control = control;
		this.dimension = dimension;
	}

	@Override
	public void paintGraph(DirectedGraph<Node, PDEEdge> g) {

		VisualizationViewer<Node, PDEEdge> vv = control.getGraphEditor()
				.getVisualizationViewer();
		vv.getGraphLayout().setGraph(g);

		int maxX = (int) (vv.getSize().width - (vv.getSize().width * 0.2));
		int maxY = (int) (vv.getSize().height - (vv.getSize().height * 0.2));

		Point2D point = new Point();
		SortCollectionsToArray sca = new SortCollectionsToArray();
		ArrayList<Node> cascadeNodes = sca.sortNodes(g.getVertices());

		int xInterval = maxX / dimension;
		int yInterval = maxY / 7;

		Node n0 = cascadeNodes.get(0);
		point.setLocation((xInterval * (dimension + 1) / 2), yInterval);
		vv.getGraphLayout().setLocation(n0, point);

		Node n1 = cascadeNodes.get(1);
		point.setLocation((xInterval * (dimension + 1) / 2), yInterval * 2);
		vv.getGraphLayout().setLocation(n1, point);

		Node n2 = cascadeNodes.get(cascadeNodes.size() - 2);
		point.setLocation((xInterval * (dimension + 1) / 2), yInterval * 6);
		vv.getGraphLayout().setLocation(n2, point);

		Node n3 = cascadeNodes.get(cascadeNodes.size() - 1);
		point.setLocation((xInterval * (dimension + 1) / 2), yInterval * 7);
		vv.getGraphLayout().setLocation(n3, point);

		int y = yInterval * 2;
		for (int i = 2; i < cascadeNodes.size() - 2; i++) {
			Node n = cascadeNodes.get(i);
			if ((i - 2) % dimension == 0) {
				y += yInterval;
			}
			point.setLocation((((i - 2) % dimension) + 1) * xInterval, y);
			vv.getGraphLayout().setLocation(n, point);
		}

		vv.repaint();
	}
}
